<?php

namespace App\Http\Controllers\Admin;
use App\Http\Model\staff;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

require_once 'public/org/Code/Code.php';
class LoginController extends Controller
{
    public function index()
    {
        //Crypt加密
//        $str ="123456";
//        echo Crypt::encrypt($str);
//        echo "<br>";
//        echo Crypt::decrypt("eyJpdiI6Im9RNFNtOHJ6WEU4aDhQTzdcL1BqMklBPT0iLCJ2YWx1ZSI6ImxKbkg0YlZpbStHTm52UDNUNWVRTXc9PSIsIm1hYyI6IjlkMmE3ZGUyZmI2Nzc0NTEzMDZmOGE3N2Y0NzU1MWJiYmI1YzZlYTQ5YzVjMTkzZTZkNDFlNmQ4ODRlNjdhMTYifQ==");
        //Input::all()   相当于 IS_POST
        if($input = Input::all()){
//            $staff = staff::where('staffname',$input['staffname'])->find(1);
//            dd($staff);
           // dd($input);//打印post数据
            //判断是否输入
            if(!$input['staffname']){
                return back()->with("message","输入账号！");
            }
            if(!$input['password']){
                return back()->with("message","输入密码！");
            }
            if(!$input['code']){
                return back()->with("message","输入验证码！");
            }
            //先判断验证码
            $code = new \App\Resources\Org\Code();
            $code =  $code->get();
            if(strtoupper($input['code']) != strtoupper($code)){
                return back()->with("message","验证码输入错误");
            }
            //查找用户
            $staff = staff::where('staffname',$input['staffname'])->find(1);
            //dd($staff);
            if(!$staff){
                return back()->with("message","帐号不存在");
            }
            //先对数据库密码解密，然后跟post密码进行判断
            Crypt::encrypt(123456);//为了让解密成功运行
            $str = $staff['attributes']['password'];
//            echo $str;
            $password = Crypt::decrypt($str);
//            echo $password;
            if($input['password'] != $password){
                return back()->with('message',"密码错误");
            }
            session(['staff_id'=>$staff['attributes']['sid']]);
            session(['staff_name'=>$staff['attributes']['staffname']]);
            return redirect("admin/");
        }else{
            return view('admin.login.login');
        }
    }
    //退出
    public function outlog()
    {
        session(['staff_id'=>null]);
        return redirect("admin/login");
    }

    // 调用验证码方法
    public function code()
    {
        $code = new \App\Resources\Org\Code();
         $code -> make();
    }
    //获得验证码值
    public function getCode()
    {
        $code = new \App\Resources\Org\Code();
        echo $code->get();
    }
}
